import collections
import sys
import spacy

# 获取命令行命令
command = sys.argv


if len(command) == 1 or command[1] == "-h":
    print("----------------命令帮助信息----------------")
    print("python wf.py -h: 显示帮助信息")
    print("python wf.py -c: 计算26个字母出现的频率")
    print("python wf.py -f: 按降序打印所有单词出现的次数")
    print("python wf.py -f -n (-n后面输入数字x): 打印出现次数最高的前x个单词")
    print("python wf.py -s: 打印出现频率最高的前10个短语")
    print("python wf.py -s -p (-p后面输出数字x): 打印出现频率最高的前10个由x个单词组成的短语")
    print("-------------------------------------------")

# 计算字母出现频率
elif command[1] == "-c":
    with open('set/text.txt', 'r', encoding='UTF-8') as f:
        strs = str(f.readlines())
        # print(str)
        # 所有字母出现的总次数
        sum = 0
        # 计数
        cnt = collections.defaultdict(int)

        for s in strs:
            if 'a' <= s.lower() <= 'z':
                sum += 1
                cnt[s.lower()] += 1

        for s in sorted(cnt, key = cnt.get, reverse=True):
            if cnt[s] >= 0:
                print("%s:%d %.2f%%"%(s, cnt[s], cnt[s] / sum * 100))


# 计算所有单词出现次数
elif command[1] == "-f":
    cnt = collections.defaultdict(int)
    nlp = spacy.load("en_core_web_sm")

    with open('set/test.txt', 'r', encoding='UTF-8') as f:
        # 获取单词列表
        strs = [word.lower() for line in f for word in line.split()]
        # print(strs)
        stop = open('set/stopwords.txt', 'r')
        stopWords = [word for line in stop for word in line.split()]
        # print(stopWords)

    for s in strs:
        doc = nlp(s)
        if s.isalpha() and s not in stopWords:
            cnt[doc[0].lemma_] += 1

    num = -1
    if len(command) > 2 and command[2] == "-n":
        num = int(command[3])

    for s in sorted(cnt, key = cnt.get, reverse=True):
        if num == -1:
            if cnt[s] >= 0:
                print("%s:%d"%(s, cnt[s]))
        else:
            if cnt[s] >= 0 and num > 0:
                print("%s:%d"%(s, cnt[s]))
                num -= 1


elif command[1] == "-s":
    cnt = collections.defaultdict(int)
    with open('set/text.txt', 'r', encoding='UTF-8') as f:
        sentence = f.read()
        sentence = str(sentence).split(",")

        # 短语总数
        sum = 0
        for sten in sentence:
            sum += 1
            cnt[sten.replace("\n", "")] += 1

        # 默认输出前10个
        num = 10

        flag = False
        if len(command) > 2 and command[2] == "-p":
            # 输出所有长度为num的短语
            flag = True
            lens = int(command[3])


        for s in sorted(cnt, key = cnt.get, reverse=True):
            if flag == False:
                if cnt[s] >= 0 and num > 0:
                    print("%s:%d %.2f%%"%(s, cnt[s], cnt[s] / sum * 100))
                    num -= 1
            else:
                if cnt[s] >= 0 and len(str(s).split(" ")) == lens and num > 0:
                    print("%s:%d %.2f%%"%(s, cnt[s], cnt[s] / sum * 100))
                    num -= 1

else:
    print("命令输入错误！")